package com.xcgame.s;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.util.HashMap;
import java.util.StringTokenizer;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.system.email.Email;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareActivity extends AppCompatActivity implements View.OnClickListener {

    private static ShareActivity instance;

    private View v_background;
    private LinearLayout ll_content;

    private ObjectAnimator cStarAnimator;
    private ObjectAnimator cEndAnimator;
    private ObjectAnimator bStarAnimator;
    private ObjectAnimator bEndAnimator;

    private LinearLayout ll_qq;
    private LinearLayout ll_qzone;
    private LinearLayout ll_wechat;
    private LinearLayout ll_wechatmoments;
    private LinearLayout ll_sinaweibo;
    private LinearLayout ll_shortmessage;
    private LinearLayout ll_email;

    private Button b_cancel;
    /**
     * 生命状态 , 0：未开始，1：开始，2：结束
     */
    private int lifeState = 0;

    private String shareTitle; //分享标题
    private String shareTitleUrl; //网友点进链接后，可以看到分享的详情
    private String shareContent; //分享文本
    private String shareImageUrl;
    private String shareImagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        instance = this;

        shareTitle = getIntent().getStringExtra("shareTitle");
        shareTitleUrl = getIntent().getStringExtra("shareTitleUrl");
        shareContent = getIntent().getStringExtra("shareContent");
        shareImageUrl = getIntent().getStringExtra("shareImageUrl");
        shareImagePath = getIntent().getStringExtra("shareImagePath");

        setContentView(R.layout.activity_share);
        initView();
        initAnimator();
        starting();
    }

    public static void start(Activity activity, String shareTitle, String shareTitleUrl, String shareContent, String shareImageUrl) {
        if (instance == null) {
            Intent intent = new Intent(activity, ShareActivity.class);
            intent.putExtra("shareTitle", shareTitle);
            intent.putExtra("shareTitleUrl", shareTitleUrl);
            intent.putExtra("shareContent", shareContent);
            intent.putExtra("shareImageUrl", shareImageUrl);
            activity.startActivity(intent);
            activity.overridePendingTransition(0, 0);
        } else {
            instance.finish();
            instance.overridePendingTransition(0, 0);
            start(activity, shareTitle, shareTitleUrl, shareContent, shareImageUrl);
        }
    }

    public static void start(Activity activity, String shareTitle, String shareTitleUrl, String shareContent, File imageFile) {
        if (instance == null) {
            Intent intent = new Intent(activity, ShareActivity.class);
            intent.putExtra("shareTitle", shareTitle);
            intent.putExtra("shareTitleUrl", shareTitleUrl);
            intent.putExtra("shareContent", shareContent);
            intent.putExtra("shareImagePath", imageFile.getPath());
            activity.startActivity(intent);
            activity.overridePendingTransition(0, 0);
        } else {
            instance.finish();
            instance.overridePendingTransition(0, 0);
            start(activity, shareTitle, shareTitleUrl, shareContent, imageFile);
        }
    }

    private void initView() {
        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        v_background = findViewById(R.id.v_background);

        ll_qq = (LinearLayout) findViewById(R.id.ll_qq);
        ll_qq.setOnClickListener(this);
        ll_qzone = (LinearLayout) findViewById(R.id.ll_qzone);
        ll_qzone.setOnClickListener(this);
        ll_wechat = (LinearLayout) findViewById(R.id.ll_wechat);
        ll_wechat.setOnClickListener(this);
        ll_wechatmoments = (LinearLayout) findViewById(R.id.ll_wechatmoments);
        ll_wechatmoments.setOnClickListener(this);
        ll_sinaweibo = (LinearLayout) findViewById(R.id.ll_sinaweibo);
        ll_sinaweibo.setOnClickListener(this);
        ll_shortmessage = (LinearLayout) findViewById(R.id.ll_shortmessage);
        ll_shortmessage.setOnClickListener(this);
        ll_email = (LinearLayout) findViewById(R.id.ll_email);
        ll_email.setOnClickListener(this);

        b_cancel = (Button) findViewById(R.id.b_cancel);
        b_cancel.setOnClickListener(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Override
    public void onBackPressed() {
        ending();
    }

    /**
     * 启动这个Activity时执行
     */
    private void starting() {
        cStarAnimator.start();
        bStarAnimator.start();
    }

    /**
     * 关闭这个Activity时执行
     */
    private void ending() {
        if (lifeState == 1) {
            cEndAnimator.start();
            bEndAnimator.start();
            lifeState = 2;
        }
    }

    private void initAnimator() {
        cStarAnimator = ObjectAnimator.ofFloat(ll_content, "translationY", measureHeight(ll_content), 0);
        bStarAnimator = ObjectAnimator.ofFloat(v_background, "alpha", 0, 1f);
        bStarAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                lifeState = 1;
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        cEndAnimator = ObjectAnimator.ofFloat(ll_content, "translationY", 0, measureHeight(ll_content));
        bEndAnimator = ObjectAnimator.ofFloat(v_background, "alpha", 1f, 0);
        bEndAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int height = ll_content.getTop();
        int y = (int) event.getY();
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (y < height) {
                ending();
            }
        }
        return true;

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_qq) {
            shareQq();
        }
        if (id == R.id.ll_qzone) {
            shareQzon();
        }
        if (id == R.id.ll_wechat) {
            shareWechat();
        }
        if (id == R.id.ll_wechatmoments) {
            shareWechatMoments();
        }
        if (id == R.id.ll_shortmessage) {
            shareShortMessage();
        }

        if(id == R.id.ll_sinaweibo){
            shareSinaweibo();
        }

        if (id == R.id.ll_email) {
            shareEmail();
        }

        if (id == R.id.b_cancel) {
            ending();
        }

    }

    private void shareQq() {
        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new QQ.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }

    private void shareQzon() {
        Platform platform = ShareSDK.getPlatform(QZone.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new QZone.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }

    private void shareWechat() {
        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new Wechat.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }



    private void shareWechatMoments() {
        Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new WechatMoments.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }

    private void shareSinaweibo(){
        Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new SinaWeibo.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }


    private void shareShortMessage() {
        Platform platform = ShareSDK.getPlatform(ShortMessage.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new ShortMessage.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }

    private void shareEmail() {
        Platform platform = ShareSDK.getPlatform(Email.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                handler.sendEmptyMessage(3);
            }
        });
        Platform.ShareParams shareParams = new Email.ShareParams();
        if (shareTitle != null) {
            shareParams.setTitle(shareTitle);
        }
        if (shareContent != null) {
            shareParams.setText(shareContent);
        }
        if (shareTitleUrl != null) {
            shareParams.setTitleUrl(shareTitleUrl);
        }
        if (shareImageUrl != null) {
            shareParams.setImageUrl(shareImageUrl);
        }
        if (shareImagePath != null) {
            shareParams.setImagePath(shareImagePath);
        }
        platform.share(shareParams);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Toast.makeText(ShareActivity.this, "分享成功", Toast.LENGTH_LONG).show();
            }
            if (msg.what == 2) {
                Toast.makeText(ShareActivity.this, "分享失败", Toast.LENGTH_LONG).show();
            }
            if (msg.what == 3) {
                Toast.makeText(ShareActivity.this, "取消分享", Toast.LENGTH_LONG).show();
            }
        }
    };

    /**
     * 测量View的高度
     *
     * @param view
     * @return
     */
    public float measureHeight(View view) {
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(0, height);
        return view.getMeasuredHeight();
    }

    /**
     * 测量View的宽带
     *
     * @param view
     * @return
     */
    public float measureWidth(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, 0);
        return view.getMeasuredWidth();
    }
}
